import socket

# 创建一个TCP/IP套接字
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # 连接到服务器
    server_address = ('localhost', 12345)
    sock.connect(server_address)
    
    # 发送数据
    message = b'Hello, world!'
    sent = sock.sendall(message)
    
except socket.error as e:
    print(f"An error occurred: {e}")
finally:
    # 关闭套接字连接
    sock.close()